package com.talkyun.openx.ocean.wrapper;

import com.talkyun.ocean.model.Service;
import com.talkyun.openx.server.core.Interceptor;
import com.talkyun.openx.server.core.ServiceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RegistryableServiceFactory implements ServiceFactory {
    private static final Logger logger = LoggerFactory.getLogger(RegistryableServiceFactory.class);
    private static final Map<String, Service> m2s = new ConcurrentHashMap<String, Service>();
    private static final Map<String, List<Interceptor>> m2i = new ConcurrentHashMap<String, List<Interceptor>>();
    private List<Service> services;
    List<Interceptor> interceptors;

    public RegistryableServiceFactory(List<Service> list, List<Interceptor> interceptors) {
        this.services = list;
        this.interceptors = interceptors;
        for (Service info : list) {
            String mapping = info.getServiceMapping().toLowerCase();
            m2s.put(mapping, info);
            logger.info("export {} to {} by {} ", info.getInterfaceName(), mapping, info.getServiceInstance());
        }
    }

    @Override
    public Object getService(String mapping) {
        Service info = m2s.get(mapping.toLowerCase());
        return info == null ? null : info.getServiceInstance();
    }

    public Service getServiceInfo(String mapping) {
        return m2s.get(mapping);
    }

    @Override
    public List<Interceptor> getInterceptor(String mapping) {
        return interceptors;
    }

    @Override
    public void add(String mapping, Object service) {

    }

    @Override
    public void add(String mapping, List<Interceptor> list, Object service) {

    }
}